<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../css/login.css">
    <link rel="stylesheet" href="../../bootstarp/css/bootstrap.min.css">
    <link rel="stylesheet" href="../css/base.css">
    <script src="../../html/Ajax.js"></script>
    <script src="../js/jquery.min.js"></script>
    <script src="../../html/设置Cookie方法.js"></script>
    <script src="../../html/获得Cookie方法.js"></script>
</head>

<body>
    <h2>Login</h2>
    <div class="container">
        <form class="form-horizontal">
            <div class="form-group">
                <label for="username" class="col-sm-2 control-label">用户名：</label>
                <div class="col-sm-10">
                    <input type="username" class="form-control" id="username" placeholder="请输入用户名"> 
                    <i id="nmtip">用户名只能为中文长度为2-10</i>
                </div>
            </div>
            <div class="form-group">
                <label for="password" class="col-sm-2 control-label">密码：</label>
                <div class="col-sm-10">
                    <input type="password" class="form-control" id="password" placeholder="请输入密码">
                    <i id="passtip">密码为3到16位（字母，数字，下划线，减号）</i>
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                    <button type="submit" class="btn btn-default">登录</button>
                    <span class="sp">忘记密码？</span>
                </div>
            </div>
        </form>
    </div>

    <script>
        let login = getCookie('login');
        // 拿到跳转之前的地址之后，需要删除localStorage的url
        let ul = localStorage.getItem('url');
        localStorage.removeItem('url');
        // console.log((ul));
        // console.log(location.href);
        // 有登陆的情况下 直接到列表页
        if (login) {
            location.href = './shouye.html';
        }
        // 没有登录，就登录
        let nm = document.querySelector('#username');
        let pass = document.querySelector('#password');
        let form = document.querySelector('form');
        let reg1 = /^[\u4E00-\u9FA5]{2,10}$/;
        let reg2 = /^[a-zA-Z0-9_-]{3,16}$/;
       

        form.onsubmit = function (e) {
            e.preventDefault();
            let flag;
            if(!reg1.test(nm.value)){
               console.log('用户名不正确');
               $('#nmtip').show();
                return;
            }
            else{
                $('#nmtip').hide();
            }
            if(!reg2.test(pass.value)){
                console.log('密码不正确');
                $('#passtip').show();
                return;
            }else{
                $('#passtip').hide();
            }
            
            pAjax({
                url: '../../6.5login.php',
                type: "post",
                data: {
                    username: nm.value,
                    password: pass.value,
                },
            }).then(res => {
                res = JSON.parse(res);
                if (!res.code) {
                    alert('用户名或者密码错误，请重新输入');
                    nm.value = pass.value = '';
                    return;
                }
                setCookie('login', nm.value);
                // let ul = localStorage.getItem('url');
                // 拿到跳转之前的地址之后，需要删除localStorage的url
                // localStorage.removeItem('url');
                // 如果localStorage的url没有，那就登录之后跳转到 列表页
                console.log(ul);
                ul ? location.href = ul : location.href = './shouye.html';

            });
        }
    </script>
</body>

</html>